package com.shsxt.tokenlxc;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.javassist.ClassClassPath;
import org.apache.ibatis.javassist.ClassPool;
import org.apache.ibatis.javassist.CtClass;
import org.apache.ibatis.javassist.CtMethod;
import org.apache.ibatis.javassist.NotFoundException;
import org.apache.ibatis.javassist.bytecode.CodeAttribute;
import org.apache.ibatis.javassist.bytecode.LocalVariableAttribute;
import org.apache.ibatis.javassist.bytecode.MethodInfo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shsxt.dao.ControllDao;
import com.shsxt.dao.WodeDao;
import com.shsxt.model.UserInfo;
import com.shsxt.service.ControllService;
@Component
public class AuthTokenAOPInterceptor {

@Resource
private ControllService controllService;
@Autowired
private WodeDao wodeDao;

public Map<String, Object> before(JoinPoint joinPoint, AuthToken authToken) throws Throwable{
	 String classType = joinPoint.getTarget().getClass().getName();    
     Class<?> clazz = Class.forName(classType);    
     String clazzName = clazz.getName();    
     String methodName = joinPoint.getSignature().getName(); //获取方法名称   
     Object[] args = joinPoint.getArgs();//参数  
       //获取参数名称和值  
     Map<String,Object > nameAndArgs = getFieldsName(this.getClass(), clazzName, methodName,args);   
     Map<String,Object> map=new HashMap<String,Object>();
     String phoneNumber="";
     String openid="";
     String token=(String) nameAndArgs.get("token");
     System.err.println(token);
     if(nameAndArgs.get("phoneNumber")!=null){
    	 phoneNumber=(String) nameAndArgs.get("phoneNumber");
    	 UserInfo user= wodeDao.findUserByPhone(phoneNumber);
    	 if(!user.getToken().equals(token)){
   		  new UserNotFoundException().f();

    	 }
     }else if(nameAndArgs.get("openid")!=null){
    	 openid=(String) nameAndArgs.get("openid");
    	 UserInfo user= wodeDao.findUserByOpenid(openid);
    	 if(!user.getToken().equals(token)){
    		  new UserNotFoundException();
    	 }
     }else{
    	  new UserNotFoundException();
     }
	return map;

    
   

}

	
	private Map<String,Object> getFieldsName(Class cls, String clazzName, String methodName, Object[] args) throws NotFoundException {   
        Map<String,Object > map=new HashMap<String,Object>();  
          
        ClassPool pool = ClassPool.getDefault();    
        //ClassClassPath classPath = new ClassClassPath(this.getClass());    
        ClassClassPath classPath = new ClassClassPath(cls);    
        pool.insertClassPath(classPath);    
            
        CtClass cc = pool.get(clazzName);    
        CtMethod cm = cc.getDeclaredMethod(methodName);    
        MethodInfo methodInfo = cm.getMethodInfo();  
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();    
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);    
        if (attr == null) {    
            // exception    
        }    
       // String[] paramNames = new String[cm.getParameterTypes().length];    
        int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;    
        for (int i = 0; i < cm.getParameterTypes().length; i++){    
            map.put( attr.variableName(i + pos),args[i]);//paramNames即参数名    
        }    
          
        //Map<>  
        return map;    
    }    
	 @AfterThrowing(value="execution(public int com.yl.spring.aop.ArithmeticCalculator.*(..))", throwing="ex")
	      public void afterThrowing(JoinPoint joinPoint, Exception ex) {
	        String methodName = joinPoint.getSignature().getName();
	         System.out.println("The method " + methodName + " occurs exception: " + ex);
	      }
}
